package com.hejing.object类.Object类之equals方法;

import java.util.Objects;

/**
 * ClassName: Account
 * Package: com.hejing.object类.Object类之equals方法
 * Description:
 *
 * @Author: HeJing
 * @Create: 2023-03-26 22:41
 * @Version: 1.0
 */
public class Account {
    private double balance;  // 余额

    public Account() {
    }

    public Account(double balance) {
        this.balance = balance;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    // 方式1: 重写equals()方法:
    /*@Override
    public boolean equals(Object obj) {
        if(this == obj) return true;
        if(obj == null) return false;
        if(obj instanceof Account) {
            Account obj_accont = (Account) obj;
            return obj_accont.getBalance() == this.balance;
        }else return false;
    }*/

    // 方式2: 调用IDEA的自动方法


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Account account = (Account) o;
        return Double.compare(account.balance, balance) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(balance);
    }
}














